#!/bin/bash
# Created on 20100623 by Jimmy Wu
# Plots an svg of the given gss along with the intersection vertices
# chosen by the selected algorithms using gunplot.
#
# Environment:
#   CLASSPATH	must include FracToDouble
#
# Dependencies:
#   select_rows.sh
#   gss_visualizer.sh
#   greedy
#   ilp_rows
#   gss.FracToDouble

usage() {
    echo Usage: "$(basename "$0")" [OPTION]... gss_file matrix_file intersections_file
    echo Plots the segments and the solutions using the specified algorithms
    echo
    echo '  -A	algorithms to plot (o=ILP, 1=sc, 2=vc, 3=ivc, 4=isvc)'
    echo '  -c	comma-separated color list for points'
    echo '  -p	comma-separated list of point sizes'
    echo '  -t	comma-separated (int) type list for points'
    echo '  -w	linewidth'
    echo '  -x  visualizer script to use'
}

# defaults
algorithms=( o )
unset linewidthargs
unset pointsizeargs
colors=( green red blue purple orange )
pointtype=( 7 6 12 4 6 )
pointsize=( .6 .9 1.2 1.5 1.8 )
visualizer=gss_visualizer.sh

while getopts :w:A:t:c:p:x: opt ; do
    case $opt in
	w) linewidthargs=(-w "$OPTARG") ;;
	A) algorithms=( $(echo "$OPTARG" | sed -e 's/[^[:alnum:]]//g' -e 's/./\0 /g') ) ;;
	t) IFS=" ," pointtype=( $OPTARG ) ;;
	c) IFS=" ," colors=( $OPTARG ) ;;
	p) IFS=" ," pointsize=( $OPTARG ) ;;
	x) visualizer="$OPTARG" ;;
	:) echo Option -"$OPTARG" requires an argument.; exit 1;;
	\?) echo Invalid option: -"$OPTARG"; exit 1;;
    esac
done

shift $(($OPTIND - 1))

if [ $# -lt 3 ] ; then
    usage >&2
    exit 1
fi

gss="$1"
matrix="$2"
intersections="$3"
base="${gss%.gss}"

fractodouble=gss.FracToDouble

ilpsol="$base".ilp.solution
scsol="$base".sc.solution
vcsol="$base".vc.solution
ivcsol="$base".ivc.solution
iscsol="$base".isc.solution

unset ilp
unset g1
unset g2
unset g3
unset g4

for a in "${algorithms[@]}" ; do
    case $a in 
	o) ilp=yes ;;
	1)  g1=yes ;;
	2)  g2=yes ;;
	3)  g3=yes ;;
	4)  g4=yes ;;
	*) echo Unrecognized algorithm: $a >&2 ;;
    esac
done

if [ ${#algorithms[@]} -gt ${#colors[@]} ] || \
    [ ${#algorithms[@]} -gt ${#pointtype[@]} ] || \
    [ ${#algorithms[@]} -gt ${#pointsize[@]} ] ; then
    echo Insufficient color and/or pointtype and/or pointsize arguments. >&2
    echo A color and pointtype must be specified for each argument >&2
    exit 1
fi

i=0

if [ -n "$ilp" ] ; then
    ilp_rows "$matrix" | select_rows.sh "$intersections" > "$ilpsol"
    if grep -q '/' "$ilpsol" ; then
	java $fractodouble "$ilpsol" "$ilpsol".double
	ilpsol="$ilpsol".double
    fi
    solution_args=( "${solution_args[@]}" "$ilpsol" "${colors[i]}" "${pointtype[i]}" "${pointsize[i]}" )
    i=$((i+1))
fi

if [ -n "$g1" ] ; then
    greedy b sc < "$matrix" | select_rows.sh "$intersections" > "$scsol"
    if grep -q '/' "$scsol" ; then
	java $fractodouble "$scsol" "$scsol".double
	scsol="$scsol".double
    fi
    solution_args=( "${solution_args[@]}" "$scsol" "${colors[i]}" "${pointtype[i]}" "${pointsize[i]}" )
    i=$((i+1))
fi

if [ -n "$g2" ] ; then
    greedy b vc < "$matrix" | select_rows.sh "$intersections" > "$vcsol"
    if grep -q '/' "$vcsol" ; then
	java $fractodouble "$vcsol" "$vcsol".double
	vcsol="$vcsol".double
    fi
    solution_args=( "${solution_args[@]}" "$vcsol" "${colors[i]}" "${pointtype[i]}" "${pointsize[i]}" )
    i=$((i+1))
fi

if [ -n "$g3" ] ; then
    greedy b ivc < "$matrix" | select_rows.sh "$intersections" > "$ivcsol"
    if grep -q '/' "$ivcsol" ; then
	java $fractodouble "$ivcsol" "$ivcsol".double
	ivcsol="$ivcsol".double
    fi
    solution_args=( "${solution_args[@]}" "$ivcsol" "${colors[i]}" "${pointtype[i]}" "${pointsize[i]}" )
    i=$((i+1))
fi

if [ -n "$g4" ] ; then
    greedy b isc < "$matrix" | select_rows.sh "$intersections" > "$iscsol"
    if grep -q '/' "$iscsol" ; then
	java $fractodouble "$iscsol" "$iscsol".double
	iscsol="$iscsol".double
    fi
    solution_args=( "${solution_args[@]}" "$iscsol" "${colors[i]}" "${pointtype[i]}" "${pointsize[i]}" )
    i=$((i+1))
fi

$visualizer "${linewidthargs[@]}" "${pointsizeargs[@]}" "$gss" "${solution_args[@]}"
